﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Web.Security;

namespace TireService.Website.Kunde
{
    public partial class Abonnement : System.Web.UI.Page
    {

        #region Public Properties

        public int CustomerId
        {
            get
            {
                if (ViewState["_customerid"] != null)
                    return (int)ViewState["_customerid"];
                else
                    return (int)Session["CustomerId"];
            }
            set
            {
                ViewState["_customerid"] = value;
            }
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // Test propertie
            //this.CustomerId = 10000;

            if (Page.IsPostBack != true)
            {
                
            }

            BindCustomerSubscription(db, this.CustomerId);
        }

        /// <summary>
        /// Function that bind customer subscriptions to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindCustomerSubscription(TireServiceDB db, int CustomerId)
        {
            List<CustomerSubscriptionView> customersubscription = db.GetCustomerSubscriptionViews(CustomerId);

            // Bind to gridview
            gvCustomerSubscription.DataSource = customersubscription;
            gvCustomerSubscription.DataBind();
        }


        #region GridView Functions

        /// <summary>
        /// Gridview function that change field data for unbound fields
        /// </summary>
        protected void gvCustomerSubscription_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            // Format UnBoundColumn SubscriptionDescription: Show subscription description
            if (e.Column.FieldName == "SubscriptionDescription")
            {
                // Get subscriptionid
                int subscriptionid = Convert.ToInt32(e.GetListSourceFieldValue("SubscriptionId"));

                TireServiceDB db = new TireServiceDB();

                // Change field value
                e.Value = db.GetSubscriptionDescription(subscriptionid);

                db.Dispose();
            }

            //// Format UnBoundColumn CurrencyCodeValue: Show currencycode
            //if (e.Column.FieldName == "CurrencyCodeValue")
            //{
            //    // Get currencyid
            //    int currencyid = Convert.ToInt32(e.GetListSourceFieldValue("CurrencyId"));

            //    TireServiceDB db = new TireServiceDB();

            //    // Change field value
            //    e.Value = db.GetCurrencyCode(currencyid);

            //    db.Dispose();
            //}
        }

        #endregion
    }
}